package com.shengqin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.entity.*;
import com.shengqin.mapper.ClassesMapper;
import com.shengqin.service.IClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengqin.service.IStudentService;
import com.shengqin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 班级信息 服务实现类
 * </p>
 *
 * @author Rui.Zhou
 * @since 2023-12-05
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements IClassesService {

    @Resource
    private IUserService userService;
    @Resource
    private UserRelSchoolService userRelSchoolService;

    @Override
    public Page<Classes> getPage(CommonPage<Classes> pageParam) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        Classes classesParam = pageParam.getQueryParam();
        if(StringUtils.hasLength(classesParam.getName())){
            wrapper.lambda().like(Classes::getName,classesParam.getName());
        }
        if(StringUtils.hasLength(classesParam.getCode())){
            wrapper.lambda().eq(Classes::getCode,classesParam.getCode());
        }
        if(classesParam.getGradeId() != null){
            wrapper.lambda().eq(Classes::getGradeId,classesParam.getGradeId());
        }
        if(classesParam.getSchoolId() != null){
            wrapper.lambda().eq(Classes::getSchoolId,classesParam.getSchoolId());
        }

        if (!StpUtil.getLoginId().equals("1")) {
            User user = userService.getById(StpUtil.getLoginId().toString());
            List<UserRelSchool> schools = userRelSchoolService.listByUserId(user.getId());
            if (CollectionUtils.isEmpty(schools)) {
                return new Page<>();
            }
            wrapper.lambda().in(Classes::getSchoolId,schools.stream().map(UserRelSchool::getSchoolId).collect(Collectors.toSet()));
        }

        wrapper.lambda().orderByAsc(Classes::getName);
        Page<Classes> classesPage = new Page<>(pageParam.getPageNo(),pageParam.getPageSize());
        page(classesPage,wrapper);
        return classesPage;
    }

    @Override
    public Classes getByGradeAndName(Integer schoolId,Integer gradeId,String name) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Classes::getName,name);
        wrapper.lambda().eq(Classes::getGradeId,gradeId);
        wrapper.lambda().eq(Classes::getSchoolId,schoolId);
        return getOne(wrapper);
    }

    @Override
    public List<Classes> listBySchoolId(Integer schoolId) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Classes::getSchoolId,schoolId);
        return list(wrapper);
    }

    @Override
    public List<Classes> listByCodes(Collection<String> classesCodes,Integer schoolId) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Classes::getCode,classesCodes);
        wrapper.lambda().eq(Classes::getSchoolId,schoolId);
        return list(wrapper);
    }

    @Override
    public List<Classes> listBySchoolIds(List<Integer> schoolIds) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Classes::getSchoolId,schoolIds);
        return list(wrapper);
    }

    @Override
    public void removeBySchoolId(Integer schoolId) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Classes::getSchoolId,schoolId);
        remove(wrapper);
    }
}
